<?php

namespace api\controllers;

use common\models\Address;
use common\models\City;
use common\models\User;
use common\service\ConstantService;
class AddrController extends BaseController
{
    /**
     * 收货地址列表
     */
    public function actionAddressList(){
        $user_id = $this->user['id'];
        if(!$user_id){
            return $this->renderJson([],'缺少参数',ConstantService::$response_fail);
        }

        $addmodel = new Address();
        $list = $addmodel->GetAddressList($user_id);
        return $this->renderJson($list,'',ConstantService::$response_success);
//        return $this->renderJson($list,'删除失败',ConstantService::$response_fail);
    }
    /**
     * 添加/修改收货地址
     */
    public function actionAddAddress(){
        $addressDb = new Address();
        $data = $_REQUEST;
        $type = $_REQUEST['type']; //1添加   0修改
        if(($msg = $this->_validation($data)) === true){
            $addr['user_id'] = $this->user['id'];;
            $addr['name'] = $data['name'];
            $addr['mobile'] = $data['mobile'];
            $addr['province'] = $data['province'];
            $addr['city'] = $data['city'];
            $addr['area'] = $data['area'];
            $addr['district'] = $data['district'];
            $addr['status'] = $data['status'];
            $addr['updated_at'] = time();

            if($type == 1){
                $addr['created_at'] = time();
                $addressDb->setAttributes($addr);
            } else {
                $addressDb = Address::findOne(['id'=>$data['id']]);
                $addressDb->setAttributes($data);
            }

            if($addressDb->save()){
                $addid = $addressDb->attributes['id'];
                return $this->renderJson($addid,'操作成功',ConstantService::$response_success);
            }
        } else {
            return $this->renderJson([],$msg,ConstantService::$response_fail);
        }
    }

    /**
     * 修改返显
     */
    public function actionGetAddressInfo(){
        $id = $_REQUEST['id'];
        $user_id = $this->user['id'];
        $address = Address::find()->where(['id'=>$id,'user_id'=>$user_id])->asArray()->one();
        return $this->renderJson($address,'',ConstantService::$response_success);
    }

    /**
     * 删除
     * @throws \Exception
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDeleteAddress(){
        $id = $_REQUEST['id'];
        $user_id = $this->user['id'];
        $del = Address::find()->where(['id'=>$id,'user_id'=>$user_id])->one();
        if($del->delete()){
            return $this->renderJson([],'删除成功',ConstantService::$response_success);
        } else {
            return $this->renderJson([],$del,ConstantService::$response_fail);
        }
    }
    /**
     * 修改默认地址
     */
    public function actionSetDefault(){
        $id = $_REQUEST['id'];
        $userid = $this->user['id'];;

        Address::updateAll(['status'=>0,'updated_at'=>time()],['user_id'=>$userid]);
        $data = Address::findOne(['id'=>$id]);
        $data->status = 1;
        if($data->save()){
            $user = User::findOne(['id'=>$userid]);
            $user->address_id = $id;
            if($user->save()){
                return $this->renderJson([],'操作成功',ConstantService::$response_success);
            }
            return $this->renderJson([],'操作失败',ConstantService::$response_fail);
        } else {
            return $this->renderJson([],'操作失败',ConstantService::$response_fail);
        }
    }

    /**
     * 获取省份
     */
    public function actionGetCity(){
        $type = $_REQUEST['type'];
        if($type == 1){

            $pro = City::find()->where(['city_id'=>0])->asArray()->all();

        } elseif($type == 2) {
            $id = $_REQUEST['id'];
            $pro = City::find()->where(['province_id'=>$id,'area_id'=>0])->asArray()->all();
        } elseif($type == 3)
        {
            $id = $_REQUEST['id'];
            $pro = City::find()->where(['city_id'=>$id])->asArray()->all();
        }

        return $this->renderJson($pro,'',ConstantService::$response_success);
    }
    /**
     * 验证
     * @param array $data
     * @return bool|string
     */
    private function _validation($data = array()){
        if(empty($data['name'])){
            return "收货人不能为空";
        }
        if(empty($data['mobile'])){
            return "手机号不能为空";
        } else {
            if(!preg_match('/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|17[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$/',$data['mobile']))
            {
                return '请输入正确的手机号';
            }
        }

        if(empty($data['district'])){
            return "详细地址不能为空";
        }
        if(empty($data['city'])){
            return "收货地址不正确";
        }
        if(empty($data['province'])){
            return "收货地址不正确";
        }
        if(empty($data['area'])){
            return "收货地址不正确";
        }

        return true;
    }

 }